﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms.DataVisualization.Charting;
using System.ComponentModel;

namespace HackersDiet
{
    public interface ISeriesManipulator
    {
        IList<Series> Manipulate(Chart chart, ChartArea chartArea, Series input);
        string Name { get; }
    }

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class ActualSeriesManipulator : ISeriesManipulator
    {
        #region ISeriesManipulator Members

        public IList<Series> Manipulate(Chart chart, ChartArea chartArea, Series input)
        {
            Series s = null;

            if (chart.Series.IsUniqueName(input.Name + "-Actual"))
            {
                s = new Series(input.Name + "-Actual");
            }
            else
            {
                s = chart.Series[input.Name + "-Actual"];
            }

            chart.DataManipulator.CopySeriesValues(input.Name, s.Name);

            return new List<Series> { s };
        }

        public string Name
        {
            get { return "Actual"; }
        }

        #endregion
    }

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class AverageSeriesManipulator : ISeriesManipulator
    {
        private uint lag = 5;

        #region Accessor Methods

        public uint Lag
        {
            get
            {
                return this.lag;
            }
            set
            {
                this.lag = value;
            }
        }

        #endregion

        #region ISeriesManipulator Members

        public IList<Series> Manipulate(Chart chart, ChartArea chartArea, Series input)
        {
            Series s = null;
            
            if (input.Points.Count > this.lag)
            {
                if (chart.Series.IsUniqueName(input.Name + "-Actual"))
                {
                    s = new Series(input.Name + "-Actual");
                }
                else
                {
                    s = chart.Series[input.Name + "-Actual"];
                }

                chart.DataManipulator.FinancialFormula(FinancialFormula.ExponentialMovingAverage, this.lag.ToString(), input.Name, s.Name);
            }
            
            return new List<Series> { s };
        }

        public string Name
        {
            get { return "Average"; }
        }

        #endregion
    }
}
